#include "Scene.h"
#include "SceneManager.h"

using namespace GalliumEngine;

Scene::Scene(void)
{
	//activePerspectiveCamera = NULL;
	//activeOrthographicCamera = NULL;
}

Scene::Scene(std::string _id)
{
	this->id = _id;
	//activePerspectiveCamera = NULL;
	//activeOrthographicCamera = NULL;
}

Scene::~Scene(void)
{
}

void Scene::setClearColour(Colour c)
{
	this->clearColour = c;
}

void Scene::initialize()
{
	clearColour = Colour(1,1,1,1);

	Environment* environment = new Environment();
	this->setEnvironment(environment);

	/*Camera *camera = new Camera("PerspectiveCamera");
	camera->initialize(Vector3(0,0,50),
		90.0f,
		(float)GameInfo::getInstance()->screenWidth/(float)GameInfo::getInstance()->screenHeight,
		0.01f,
		1000.0f);
	this->addObject(camera);

	this->setActivePerspectiveCamera("PerspectiveCamera");

	Camera *uiCamera = new Camera("OrthoCamera");
	uiCamera->initialize(0,
		(float)GameInfo::getInstance()->screenWidth,
		0,
		(float)GameInfo::getInstance()->screenHeight,
		0.0f,
		1.0f);
	this->addObject(uiCamera);

	this->setActiveOrthographicCamera("OrthoCamera");*/
}

void Scene::deinitialize()
{
	for (objects_map_const_itr itr = objects.begin(); itr != objects.end(); ++itr)
	{
		//itr->second->deinitialize();
		delete itr->second; 
	}

	objects.clear();

	/*for (ui_map_const_itr itr = ui.begin(); itr != ui.end(); ++itr)
	{
		itr->second->deinitialize();
		delete itr->second; 
	}*/

	objects.clear();

	delete environment;

	ResourceManager::getInstance()->release(this->getId());
}

std::string Scene::getId() const
{
	return id;
}

Environment* Scene::getEnvironment()
{
	return environment;
}

void Scene::setEnvironment(Environment* _environment)
{
	environment = _environment;
}

void Scene::addObject(GameObject *object)
{
	//object->setScene(this);
	//object->setParent(NULL);
	this->objects.insert(objects_pair(object->getId(),object));
}

void Scene::removeObject(std::string _id)
{
	objects_map_itr itr = this->objects.find(_id);
	this->objects.erase(itr); 
}

//void Scene::addUIControl(UIControl *ui)
//{
//	ui->setScene(this);
//	ui->setParent(NULL);
//	this->ui.insert(ui_pair(ui->getId(),ui));
//}
//
//void Scene::removeUIControl(std::string _id)
//{
//	ui_map_itr itr = this->ui.find(_id);
//	this->ui.erase(itr); 
//}

GameObject* Scene::getObject(std::string _id)
{
	objects_map_const_itr itr = objects.find(_id);

	if(itr != objects.end())
	{
		return (itr->second);
	}
	else
	{
		return NULL;
	}
}

//UIControl* Scene::getUIControl(std::string _id)
//{
//	ui_map_const_itr itr = ui.find(_id);
//
//	if(itr != ui.end())
//	{
//		return (itr->second);
//	}
//	else
//	{
//		return NULL;
//	}
//}

//Camera* Scene::getActivePerspectiveCamera()
//{
//	return activePerspectiveCamera;
//}
//
//void Scene::setActivePerspectiveCamera(std::string id)
//{
//	activePerspectiveCamera = (Camera*)objects[id];
//}
//
//Camera* Scene::getActiveOrthographicCamera()
//{
//	return activeOrthographicCamera;
//}
//
//void Scene::setActiveOrthographicCamera(std::string id)
//{
//	activeOrthographicCamera = (Camera*)objects[id];
//}


void Scene::update(float delta)
{
	objects_map deferred;

	//if(activePerspectiveCamera != NULL)
	//{

	//	for(objects_map_itr itr = objects.begin(); itr != objects.end(); ++itr)
	//	{
	//		/*if(itr->second->getRenderType() == RENDER_TYPE::DEFERRED)
	//		{*/
	//			deferred.insert(objects_pair(itr->second->getId(),itr->second));
	//		/*}
	//		else
	//		{
	//			itr->second->update(delta);
	//		}*/
	//	}

	//	for(objects_map_itr itr = deferred.begin(); itr != deferred.end(); ++itr)
	//	{
	//		itr->second->update(delta);
	//	}

	//	deferred.clear();
	//}

	/*if(activeOrthographicCamera != NULL)
	{
		for(ui_map_itr itr = ui.begin(); itr != ui.end(); ++itr)
		{
			itr->second->update(delta);
		}
	}*/
}

void Scene::draw()
{
	glClearColor(clearColour.getR(),clearColour.getG(),clearColour.getB(),clearColour.getA());

	for(int i = 0; i < this->getEnvironment()->getNumPointLights(); i++)
	{
		this->getEnvironment()->getPointLight(i)->draw();
	}

	glEnable(GL_TEXTURE_2D);

	//if(activePerspectiveCamera != NULL)
	//{
	//	objects_map deferred;

	//	for(objects_map_itr itr = objects.begin(); itr != objects.end(); ++itr)
	//	{
	//		/*if(itr->second->getRenderType() == RENDER_TYPE::DEFERRED)
	//		{*/
	//			deferred.insert(objects_pair(itr->second->getId(),itr->second));
	//		/*}
	//		else
	//		{
	//			itr->second->draw();
	//		}*/
	//	}

	//	for(objects_map_itr itr = deferred.begin(); itr != deferred.end(); ++itr)
	//	{
	//		itr->second->draw();
	//	}

	//	deferred.clear();

	//}

	/*if(activeOrthographicCamera != NULL)
	{
		glEnable(GL_TEXTURE_2D);
		glDisable(GL_DEPTH_TEST);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

		for(ui_map_itr itr = ui.begin(); itr != ui.end(); ++itr)
		{
			itr->second->draw();
		}

		glDisable(GL_BLEND);
		glDisable(GL_TEXTURE_2D);
		glEnable(GL_DEPTH_TEST);
	}*/
}